﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Business;
using AMM.Model;
using AMM.Model.Business;

namespace AMM.Business.Business {
    public class PriceEvaluateProcessor : BaseProcessor {

        private int _pageSize;
        private PriceEvaluateRepository _priceEvaluateRepository;

        public PriceEvaluateProcessor(int pageSize) {
            this._pageSize = pageSize;
            this._priceEvaluateRepository = new PriceEvaluateRepository(pageSize);
        }

        public PriceEvaluate FetchPriceEvaluateById(int priceEvaluateId) {
            try {
                return this._priceEvaluateRepository.FetchPriceEvaluateById(priceEvaluateId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定价格体系数据失败！");
            }
        }

        public List<PriceEvaluate> FetchPriceEvaluateList() {
            try {
                return this._priceEvaluateRepository.FetchPriceEvaluateList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取价格体系列表失败！");
            }
        }

        public List<PriceEvaluateViewModel> FetchPriceEvaluateViewModelList(PriceEvaluateSearchModel searchModel, int pageIndex = 1) {
            try {
                List<PriceEvaluate> resultList = this._priceEvaluateRepository.FetchPriceEvaluateList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new PriceEvaluateViewModel() {
                        Key = x.PriceEvaluateId,
                        CustomerCode = x.Customer.Code,
                        CustomerName = x.Customer.Name,
                        PayCurrency = x.Basic.Code,
                        PaymentType = x.Basic1.Code,
                        ProductCode = x.Product.Code,
                        Price = x.Price,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取价格体系列表失败！");
            }
        }

        public void Insert(PriceEvaluate priceEvaluate) {
            try {
                priceEvaluate.PriceEvaluateId = Guid.NewGuid().GetHashCode();
                priceEvaluate.CreatedDatetime = DateTime.Now;
                this._priceEvaluateRepository.Insert(priceEvaluate);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新价格体系失败！");
            }
        }

        public void Update(PriceEvaluate priceEvaluate) {
            try {
                PriceEvaluate oldPriceEvaluate = this.FetchPriceEvaluateById(priceEvaluate.PriceEvaluateId);
                if (!oldPriceEvaluate.RowVersion.SequenceEqual(priceEvaluate.RowVersion)) {
                    throw this.BuildException("更新价格体系失败，数据不一致性错误！");
                }
                priceEvaluate.ModifiedDatetime = DateTime.Now;
                this._priceEvaluateRepository.Update(priceEvaluate);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新价格体系失败！");
            }
        }

        public void Delete(PriceEvaluate priceEvaluate) {
            try {
                this._priceEvaluateRepository.Delete(priceEvaluate);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除价格体系失败！");
            }
        }

        public void Delete(params int[] priceEvaluateIds) {
            try {
                this._priceEvaluateRepository.DeleteMultiplePriceEvaluate(priceEvaluateIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除价格体系失败！");
            }
        }
    }
}
